package
{
	import flash.events.MouseEvent;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.display.StageDisplayState;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.ServerSocketConnectEvent;
	import flash.net.InterfaceAddress;
	import flash.net.NetworkInfo;
	import flash.net.NetworkInterface;
	import flash.net.ServerSocket;
	import flash.net.Socket;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import mylist.DragItem;
	import mylist.IconItem;
	import mylist.logs;
	import mylist.ViewList;
	/**
	 * 中兴项目server document class
	 * 
	 */
	[SWF(width=1920,height=1080)]
	public class server extends Sprite
	{
		public static var main:server;
		private var serverSocket:ServerSocket = new ServerSocket();
		private var localIP:String = "127.0.0.1";
		private var localPort:uint = 843;
		
		public function server()
		{
			
			if(!DragItem.willSend){
				IconItem.ww = 250;
				IconItem.hh = 250;
			}
			
			stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
			stage.scaleMode = StageScaleMode.EXACT_FIT;
			//stage.addChild(new logs);
			[Embed(source = "asset/pcbg.jpg")]
			var bg:Class;
			addChild(new bg());
			
			var loader:URLLoader = new URLLoader(new URLRequest("net.xml"));
			loader.addEventListener(Event.COMPLETE, bind);
			
			//stage.addEventListener(MouseEvent.CLICK, clicked);
		}

		private function clicked(event : MouseEvent) : void {
			var bmp:Bitmap = new Bitmap(new BitmapData(100 * Math.random() + 2, 100 * Math.random() + 2, false, 0xffffffff * Math.random()));
			ViewList.main.addChilds(bmp);
		}
		
		private function keys(e:KeyboardEvent):void
		{
			logs.adds(String.fromCharCode(e.charCode));
			switch (String.fromCharCode(e.charCode))
			{
				case "k": 
					var bmp:Bitmap = new Bitmap(new BitmapData(100 * Math.random() + 2, 100 * Math.random() + 2, false, 0xffffffff * Math.random()));
					ViewList.main.addChilds(bmp);
					break;
				case "h": 
					addChild(new logs());
					logs.txt.visible =!logs.txt.visible;
					break;
				default: 
			}
		}
		
		private function onConnect(event:ServerSocketConnectEvent):void
		{
			var clientSocket:Socket = event.socket;
			
			var item:SocketItem = new SocketItem();
			item.init(clientSocket);
		}
		
		private function bind(e:Event):void
		{
			stage.addEventListener(KeyboardEvent.KEY_UP, keys);
			
			var data:XML = XML(URLLoader(e.target).data);
			localIP = getMyIp(String(data.ip));
			//localIP = String(data.ip);
			localPort = uint(data.port);
			
			
			main = this;
			this.stage.nativeWindow.activate();
			addEventListener(Event.ADDED_TO_STAGE, bind);
			
			addChild(new ViewList());
			addChild(new PrintBtn);
			
			
			if (serverSocket.bound)
			{
				serverSocket.close();
				serverSocket = new ServerSocket();
			}
			serverSocket.bind((localPort), localIP);
			serverSocket.addEventListener(ServerSocketConnectEvent.CONNECT, onConnect);
			serverSocket.listen();
			logs.adds("Bound to: " + serverSocket.localAddress + ":" + serverSocket.localPort);
		}
		
		 /**
         * 查找ip是不是本机的ip,如是,返回原ip,否则返回本机的其他ip
         * @param    ip 需要查找的ip地址
         * @return 一个属于本机的ip地址
         */
        private function getMyIp(ip:String):String
        {
           
            var networkInfo:NetworkInfo = NetworkInfo.networkInfo;
            var interfaces:Vector.<NetworkInterface> = networkInfo.findInterfaces();
            var ipArr:Array = new Array();
            if( interfaces != null )
            {
                for each ( var interfaceObj:NetworkInterface in interfaces )
                {
                    for each ( var address:InterfaceAddress in interfaceObj.addresses )
                    {
                       
                        if (address.ipVersion == "IPv4" && address.address != "127.0.0.1")
                        {
                            logs.adds( "  type: "           + address.ipVersion );
                            logs.adds( "  address: "         + address.address );
                           //
                            logs.adds( "  broadcast: "         + address.broadcast );
                            logs.adds( "  prefix length: "     + address.prefixLength );
                            ipArr.push(address.address);
                            //ip = address.address;
                        }
                    }
                }           
            }
           
            if (ipArr.length < 1) ip = "127.0.0.1";
            if (ipArr.length == 1) ip = ipArr[0];
            if (ipArr.length > 1) {
                var bool:Boolean = false;
                var otherIp:String;
                for each (var item:String in ipArr)
                {
                    if (ip == item) {
                        bool = true;
                        break;
                    }else {
                        otherIp = item;
                    }
                }
                if (!bool) ip = otherIp;
            }
            return ip;
        }
	
	}
}

